import { message } from 'antd';
import type { InternalAxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
import globalData from '../store/global';

/**
 * 请求拦截器
 */
export const requestInterceptor = (config: InternalAxiosRequestConfig) => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers["Authorization"] = "Bearer " + token;
  }
  return config;
};

/**
 * 请求拦截器错误
 */
export const requestInterceptorError = (error) => {
  return Promise.reject(error);
};

/**
 * 响应拦截器
 */
export const responseInterceptor = (response) => {
  return response;
};

/**
 * 响应拦截器错误
 */
export const responseInterceptorError = (error) => {
  if (error.response.data.code == 403) {
    message.error("登录过期，请重新登录")
    globalData.navigate("/login")
  }
  if (error.response.data.code != 401) {
    message.error("服务异常")
  }
  return error.response;
};
